using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.MedicalDomain;
using ShivamEMR.Model.DataInterfaces.MedicalDataInterf;
using ShivamEMR.Model.DataInterfaces.CommonDataInterf;
using ShivamEMR.Model;

namespace ShivamEMR.Web.Areas.Medical.Controllers
{   
    public class TestController : Controller
    {
		private readonly ITestGroupRepository testgroupRepository;
		private readonly IUnitRepository unitRepository;
		private readonly ITestRepository testRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public TestController() //: this(new TestGroupRepository(), new UnitRepository(), new TestRepository())
        {
            this.testgroupRepository = Registry.RepositoryFactory.GetTestGroupRepository();
            this.unitRepository = Registry.RepositoryFactory.GetUnitRepository();
            this.testRepository = Registry.RepositoryFactory.GetTestRepository();
        }

        public TestController(ITestGroupRepository testgroupRepository, IUnitRepository unitRepository, ITestRepository testRepository)
        {
			this.testgroupRepository = testgroupRepository;
			this.unitRepository = unitRepository;
			this.testRepository = testRepository;
        }

        //
        // GET: /Test/

        public ViewResult Index()
        {
            return View(testRepository.All);//Including(test => test.Group, test => test.Unit, test => test.Alerts, test => test.PatientTests));
        }

        //
        // GET: /Test/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(testRepository.Find(id));
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
			ViewBag.PossibleGroups = testgroupRepository.All;
			ViewBag.PossibleUnits = unitRepository.All;
            return View();
        } 

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(Test test)
        {
            if (ModelState.IsValid) {
                testRepository.InsertOrUpdate(test);
                testRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleGroups = testgroupRepository.All;
				ViewBag.PossibleUnits = unitRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Test/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleGroups = testgroupRepository.All;
			ViewBag.PossibleUnits = unitRepository.All;
             return View(testRepository.Find(id));
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(Test test)
        {
            if (ModelState.IsValid) {
                testRepository.InsertOrUpdate(test);
                testRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleGroups = testgroupRepository.All;
				ViewBag.PossibleUnits = unitRepository.All;
				return View();
			}
        }

        //
        // GET: /Test/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(testRepository.Find(id));
        }

        //
        // POST: /Test/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var t = testRepository.Find(id);
            testRepository.Delete(t);
            testRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

